package org.dromara.wmscloud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.wmscloud.domain.bo.StockMatLabelBo;
import org.dromara.wmscloud.domain.vo.StockMatLabelVo;
import org.dromara.wmscloud.domain.StockMatLabel;
import org.dromara.wmscloud.mapper.StockMatLabelMapper;
import org.dromara.wmscloud.service.IStockMatLabelService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物料标签Service业务层处理
 *
 * @author hou
 * @date 2025-04-12
 */
@RequiredArgsConstructor
@Service
public class StockMatLabelServiceImpl implements IStockMatLabelService {

    private final StockMatLabelMapper baseMapper;

    @Override
    public List<StockMatLabelVo> queryByIds(Long[] labelIds) {
        return baseMapper.selectVoByIds(Arrays.asList(labelIds));
    }

    /**
     * 查询物料标签
     *
     * @param labelId 主键
     * @return 物料标签
     */
    @Override
    public StockMatLabelVo queryById(Long labelId){
        return baseMapper.selectVoById(labelId);
    }

    /**
     * 分页查询物料标签列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 物料标签分页列表
     */
    @Override
    public TableDataInfo<StockMatLabelVo> queryPageList(StockMatLabelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StockMatLabel> lqw = buildQueryWrapper(bo);
        Page<StockMatLabelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 分页查询物料标签（弹窗）
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 物料标签分页列表
     */
    @Override
    public TableDataInfo<StockMatLabelVo> queryDialogList(StockMatLabelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StockMatLabel> lqw = buildDialogQueryWrapper(bo);
        Page<StockMatLabelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的物料标签列表
     *
     * @param bo 查询条件
     * @return 物料标签列表
     */
    @Override
    public List<StockMatLabelVo> queryList(StockMatLabelBo bo) {
        LambdaQueryWrapper<StockMatLabel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StockMatLabel> buildQueryWrapper(StockMatLabelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StockMatLabel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(StockMatLabel::getLabelId);
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCode()), StockMatLabel::getWarehouseCode, bo.getWarehouseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLocationCode()), StockMatLabel::getLocationCode, bo.getLocationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMatCode()), StockMatLabel::getMatCode, bo.getMatCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFigNum()), StockMatLabel::getFigNum, bo.getFigNum());
        lqw.eq(StringUtils.isNotBlank(bo.getMatGroup()), StockMatLabel::getMatGroup, bo.getMatGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getMatClass()), StockMatLabel::getMatClass, bo.getMatClass());
        lqw.eq(StringUtils.isNotBlank(bo.getBatch()), StockMatLabel::getBatch, bo.getBatch());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierName()), StockMatLabel::getSupplierName, bo.getSupplierName());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), StockMatLabel::getSupplierCode, bo.getSupplierCode());
        lqw.eq(bo.getProdTime() != null, StockMatLabel::getProdTime, bo.getProdTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), StockMatLabel::getOrderNo, bo.getOrderNo());
        return lqw;
    }

    private LambdaQueryWrapper<StockMatLabel> buildDialogQueryWrapper(StockMatLabelBo bo) {
        LambdaQueryWrapper<StockMatLabel> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(StockMatLabel::getLabelId);
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCode()), StockMatLabel::getWarehouseCode, bo.getWarehouseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getLocationCode()), StockMatLabel::getLocationCode, bo.getLocationCode());
        lqw.eq(StringUtils.isNotBlank(bo.getMatCode()), StockMatLabel::getMatCode, bo.getMatCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFigNum()), StockMatLabel::getFigNum, bo.getFigNum());
        lqw.eq(StringUtils.isNotBlank(bo.getMatGroup()), StockMatLabel::getMatGroup, bo.getMatGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getMatClass()), StockMatLabel::getMatClass, bo.getMatClass());
        lqw.eq(StringUtils.isNotBlank(bo.getBatch()), StockMatLabel::getBatch, bo.getBatch());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierName()), StockMatLabel::getSupplierName, bo.getSupplierName());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), StockMatLabel::getSupplierCode, bo.getSupplierCode());
        lqw.eq(bo.getProdTime() != null, StockMatLabel::getProdTime, bo.getProdTime());
        // 查询 orderNo 为 null 或空字符串的记录
        lqw.and(wrapper ->
            wrapper.isNull(StockMatLabel::getOrderNo)
                .or().eq(StockMatLabel::getOrderNo, "")
        );
        return lqw;
    }

    /**
     * 新增物料标签
     *
     * @param bo 物料标签
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(StockMatLabelBo bo) {
        StockMatLabel add = MapstructUtils.convert(bo, StockMatLabel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLabelId(add.getLabelId());
        }
        return flag;
    }

    /**
     * 修改物料标签
     *
     * @param bo 物料标签
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(StockMatLabelBo bo) {
        StockMatLabel update = MapstructUtils.convert(bo, StockMatLabel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StockMatLabel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除物料标签信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
